//
//  LSCorrectViewController.m
//  ZLFastDoctor
//
//  Created by 李宗帅 on 16/5/22.
//  Copyright © 2016年 LS. All rights reserved.
//

#import "LSCorrectViewController.h"
#import <Masonry.h>
#import <SVProgressHUD.h> 
#import "NQSaveTool.h"
#define LSCorrectViewControllerMargin 10

@interface LSCorrectViewController ()

//原密码
@property (nonatomic,strong) UITextField *oldPassword;

//新密码
@property (nonatomic,strong) UITextField *Password;


//再次输入密码
@property (nonatomic,strong) UITextField *Password2;


//原密码前方占位图片
@property (nonatomic,strong) UIImageView *oldPasswordImage;
//新密码前方占位图片
@property (nonatomic,strong) UIImageView *passwordImage;
//再次输入密码前方占位图片
@property (nonatomic,strong) UIImageView *passwordImage2;

//完成按钮
@property (nonatomic,strong) UIButton *finishBtn;
@end

@implementation LSCorrectViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupUI];
}

//加载控件并布局
- (void)setupUI {
    
    self.oldPassword.placeholder = @"请输入原密码";
    
    self.Password.placeholder = @"请输入新密码";
    
    self.Password2.placeholder = @"请确认新密码";
    
    self.oldPassword.borderStyle = UITextBorderStyleRoundedRect;
    
    self.oldPassword.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    self.Password.borderStyle = UITextBorderStyleRoundedRect;
    
    self.Password.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    self.Password2.borderStyle = UITextBorderStyleRoundedRect;
    
    self.Password2.clearButtonMode = UITextFieldViewModeWhileEditing;

    [self.finishBtn addTarget:self action:@selector(clickFinishBtn) forControlEvents:UIControlEventTouchUpInside];

    //添加到当前View
    [self.view addSubview:self.oldPassword];
    [self.view addSubview:self.Password];
    [self.view addSubview:self.Password2];
    [self.view addSubview:self.passwordImage];
    [self.view addSubview:self.passwordImage2];
    [self.view addSubview:self.oldPasswordImage];
    [self.view addSubview:self.finishBtn];
    
    //textFiled的宽
    CGFloat TFW = NQScreenW - 150;
    
    
    //布局子控件
    [self.oldPassword mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.width.equalTo(@(TFW));
        
        make.top.equalTo(self.view.mas_top).offset(80);
        
        make.centerX.equalTo(self.view.mas_centerX).offset(25);
        
    }];
    
    [self.oldPasswordImage mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.equalTo(self.oldPassword.mas_left).offset(-LSCorrectViewControllerMargin);
        
        make.centerY.equalTo(self.oldPassword.mas_centerY);
    }];
    
    
    [self.Password mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.left.equalTo(self.oldPassword);
        
        make.top.equalTo(self.oldPassword.mas_bottom).offset(5*LSCorrectViewControllerMargin);
        
    }];
    
    [self.passwordImage mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.equalTo(self.Password.mas_left).offset(-LSCorrectViewControllerMargin);
        make.centerY.equalTo(self.Password.mas_centerY);
    }];
    
    
    [self.Password2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.left.equalTo(self.oldPassword);
        
        make.top.equalTo(self.Password.mas_bottom).offset(5*LSCorrectViewControllerMargin);
    }];
    
    [self.passwordImage2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.Password2.mas_left).offset(-LSCorrectViewControllerMargin);
        make.centerY.equalTo(self.Password2.mas_centerY);
    }];
    
    
    [self.finishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view);
        
        make.top.equalTo(self.Password2.mas_bottom).offset(5*LSCorrectViewControllerMargin);
        make.height.equalTo(@50);
        
        make.width.equalTo(self.view.mas_width).offset(2 *  -LSCorrectViewControllerMargin);
    }];
    
}

//点击完成按钮事件
- (void)clickFinishBtn {
    
    //然后遍历原密码字符串 是否和原密码一样  如果不一样 动画报错
    NSString *oldWord = self.oldPassword.text;
    NSString *word = self.Password.text;
    NSString *newWord = self.Password2.text;
//    NSLog(@"%zd,%zd,%zd",oldWord.length,word.length,newWord.length);
    //逻辑上 遍历三个UITextFiled输入的内容的长度,如果有一个是0就要返回
    if (oldWord.length ==0 || word.length==0 || newWord.length==0) {
        [SVProgressHUD showErrorWithStatus:@"请输入密码!"];
        return;
    }
    
    //取出登录密码
    NSString *password = [NQSaveTool objectForKey:NQpassWord];
    
    if (![oldWord isEqualToString:password]) {
        [SVProgressHUD showErrorWithStatus:@"原密码输入错误"];
        return;
    }

    
    //然后匹配新密码两次输入是否相同 不相同  动画报错 两次输入不一致 [self.Password.text isEqualToString:@""] && [self.Password2.text isEqualToString:@""]
    if (![word isEqualToString:newWord]) {
        [SVProgressHUD showErrorWithStatus:@"新密码输入错误"];
        return;
    }
    
    //更改密码到偏好设置
    [NQSaveTool setObject:self.Password2.text forKey:NQpassWord];
    
    //修改密码成功
    [SVProgressHUD showSuccessWithStatus:@"修改密码成功"];
    
    //返回首界面
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    //如果有时间就加入遍历字符串,看看除字母和数字,符号之外是否还有其他的 如果有报错
    
    //判断是否一种  是  报错返回
    //判断是否有中文,有就报错
}

- (UIImageView *)oldPasswordImage {
    if (_oldPasswordImage == nil) {
        _oldPasswordImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"attention"]];
    }
    
    return _oldPasswordImage;
}

- (UITextField *)Password {
    if (_Password == nil) {
        _Password = [[UITextField alloc]init];
    }
    
    return  _Password;
}

- (UITextField *)oldPassword {
    if (_oldPassword == nil) {
        _oldPassword = [[UITextField alloc]init];
    }
    
    return _oldPassword;
}

- (UITextField *)Password2 {
    if (_Password2 == nil) {
        _Password2 = [[UITextField alloc]init];
    }
    return _Password2;
}

- (UIImageView *)passwordImage {
    if (_passwordImage == nil) {
        _passwordImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"attention"]];
    }
    
    return _passwordImage;
}

- (UIImageView *)passwordImage2 {
    if (_passwordImage2 == nil) {
        _passwordImage2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"attention"]];
    }
    return _passwordImage2;
}

- (UIButton *)finishBtn {
    if (_finishBtn == nil) {
        _finishBtn = [[UIButton alloc]init];
        
        [_finishBtn setBackgroundImage:[UIImage imageNamed:@"nav"] forState:UIControlStateNormal];
        
        [_finishBtn setTitle:@"完成" forState:UIControlStateNormal];
    }
    
    return _finishBtn;
}
@end
